﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.GDIPlus;
using System.Runtime.InteropServices;
using OpenNETCF.Net;
using FetionLite;

namespace ControlTest
{
    public partial class Form1 : Form
    {
        private int testCount = 0;

        private ConnectionManager conMgr;

        [DllImport("LibFx")]
        private static extern bool fx_init();

        [DllImport("LibFx")]
        private static extern void fx_terminate();

        [DllImport("LibFx")]
        private static extern bool fx_set_serve_address([In]string serverAddress);

        [DllImport("LibFx")]
        private static extern bool fs_login(string username, string pwd);

        [DllImport("LibFx")]
        private static extern int fx_login(byte[] mobile, byte[] pwd, FxCallBack callback, ref IntPtr arg);

        [DllImport("LibFx")]
        private static extern void fx_set_system_msg_cb(FxCallBack callback, ref IntPtr arg);

        [DllImport("LibFx")]
        private static extern bool fs_send_sms_to_self(byte[] mesage);

        [DllImport("LibFx")]
        private static extern IntPtr fx_get_group();

        [DllImport("LibFx")]
        private static extern IntPtr fx_get_first_account();

        [DllImport("LibFx")]
        private static extern IntPtr fx_get_next_account(IntPtr fetionAccount);

        [DllImport("LibFx")]
        private static extern void fx_update_account_info_by_id(int id);

        [DllImport("LibFx")]
        private static extern IntPtr fx_get_account_by_id(int id);

        public delegate void FxCallBack(int message, int wParam, int lParam, IntPtr arg);
        public delegate void CallMsg(string msg);

        const int FX_EVENT_BASE = 0x0000;
        const int FX_EVENT_SPARE = 0x00F0;

        private static byte[] StringToUTF8Bytes(string sourceString)
        {
            byte[] unicodeBytes = Encoding.Unicode.GetBytes(sourceString);
            byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes);
            byte[] destBytes = new byte[utf8Bytes.Length + 1];

            Array.Copy(utf8Bytes, destBytes, utf8Bytes.Length);

            return destBytes;
        }

        public Form1()
        {
            //GdiplusStartupInput input = new GdiplusStartupInput();
            //GdiplusStartupOutput output;
            //GpStatus stat = NativeMethods.GdiplusStartup(out token, input, out output);

            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.scrollTextPanel1.Text += string.Format("\n\r测试{0}{1}", ++testCount, DateTime.Now);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conMgr = new ConnectionManager();
            Guid guid = conMgr.MapUrl("http://221.130.45.208:8080");
            conMgr.Connect(guid, false, ConnectionMode.Synchronous);

            //bool result;
            //bool logined;
            //bool sent;

            //result = fx_init();
            //MessageBox.Show("fx_init: " + result.ToString());

            ////result = fx_set_serve_address("221.130.45.208:8080");
            ////MessageBox.Show("fx_set_serve_address: " + result.ToString());

            string mobile = "717827100";
            string pwd = "hulala";
            ////byte[] mobileArr = System.Text.Encoding.UTF8.GetBytes(mobile);
            ////byte[] pwdArr = System.Text.Encoding.UTF8.GetBytes(pwd);

            //logined = fs_login(mobile, pwd);
            //MessageBox.Show("fs_login: " + logined.ToString());

            //sent = fs_send_sms_to_self("哈哈哈，发过来了！！");
            //MessageBox.Show("fs_send_sms_to_self: " + sent.ToString());

            //byte[] b = Encoding.Unicode.GetBytes(mobile);
            //byte[] c = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, b);
            ////char[] d = new char[Encoding.UTF8.GetCharCount(c)];
            //char[] d = Encoding.UTF8.GetChars(c);
            //string str = new string(d);

            if (fx_init())
            {
                FxCallBack callback = new FxCallBack(LoginHandler);
                IntPtr pt = new IntPtr();
                GC.KeepAlive(callback);//防止进行垃圾回收
                fx_login(StringToUTF8Bytes(mobile), StringToUTF8Bytes(pwd), callback, ref pt);
            }

            //Fetion.Instance.OnLoginStateChanged += new Fetion.LoginStateChangedDelegate(Instance_OnLoginStateChange);
            //Fetion.Instance.Login(mobile, pwd);
        }

        void LoginHandler(int message, int wParam, int lParam, IntPtr arg)
        {
            CallMsg callMsg = new CallMsg(SetMessage);
            switch (message)
            {
                case FX_EVENT_SPARE + 0x0001:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "手机号或fetion号错误");
                    }
                    break;

                case FX_EVENT_BASE + 0x0001:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "正在连接服务器");
                    }
                    break;

                case FX_EVENT_BASE + 0x0002:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "正在验证用户名和密码");
                    }
                    break;

                case FX_EVENT_BASE + 0x0003:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "验证用户名和密码成功");
                    }
                    break;

                case FX_EVENT_BASE + 0x0004:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "登陆失败");
                    }
                    break;
                case FX_EVENT_BASE + 0x0005:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "网络错误");
                    }
                    break;
                case FX_EVENT_BASE + 0x0006:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "登陆出现未知错误");
                    }
                    break;

                case FX_EVENT_BASE + 0x0007:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "用户名错误");
                    }
                    break;

                case FX_EVENT_BASE + 0x0008:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "正在获取联系人列表");
                    }
                    break;

                case FX_EVENT_BASE + 0x0009:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "获取联系人列表成功");
                    }
                    break;

                case FX_EVENT_BASE + 0x000A:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "获取联系人列表失败");
                    }
                    break;

                case FX_EVENT_BASE + 0x000B:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "正在获取联系人信息");
                    }
                    break;

                case FX_EVENT_BASE + 0x000C:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "获取联系人信息成功");
                    }
                    break;
                case FX_EVENT_BASE + 0x000D:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "获取联系人信息失败");
                    }
                    break;

                case FX_EVENT_BASE + 0x000E:
                    if (this.InvokeRequired)
                    {
                        this.Invoke(callMsg, "登陆成功");
                        //SendMsgDialog msgDialog = new SendMsgDialog();
                        //msgDialog.ShowDialog();
                    }
                    //你可以在这里做一些登录成功的处理	

                    //List<FetionGroup> groupList = Fetion.Instance.GetGroupList();
                    //foreach (FetionGroup g in groupList)
                    //    Console.WriteLine(g.Name);

                    //List<FetionAccount> accountList = Fetion.Instance.GetAccountList();
                    //foreach (FetionAccount a in accountList)
                    //    Console.WriteLine(a.LocalName);

                    IntPtr p = new IntPtr();
                    FxCallBack fc = new FxCallBack(EventHandler);
                    GC.KeepAlive(fc);
                    fx_set_system_msg_cb(new FxCallBack(fc), ref p);

                    break;
                case FX_EVENT_BASE + 0x0010:
                    MessageBox.Show(lParam.ToString());
                    break;
            }
        }

        void EventHandler(int message, int wParam, int lParam, IntPtr arg)
        {
            CallMsg callMsg = new CallMsg(SetMessage);
            switch (message)
            {
                case FX_EVENT_BASE + 0x0010:
                    MessageBox.Show(lParam.ToString());
                    break;
            }
        }

        public void SetMessage(string message)
        {
            this.Text = message;
        }

        private void Form1_Closed(object sender, EventArgs e)
        {
            //NativeMethods.GdiplusShutdown(token);
            Fetion.Instance.Dispose();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
                bubbleTextPanel1.Width--;
            else if (e.KeyCode == Keys.Right)
                bubbleTextPanel1.Width++;
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //Fetion.Instance.OnNewMessage += new Fetion.NewMessageDeletegate(Instance_OnNewMessage);
            //Fetion.Instance.StartEeventListener();
        }
    }
}